package com.gdlgxy.oa.controller;

import com.gdlgxy.oa.bean.Fare;
import com.gdlgxy.oa.bean.FareList;
import com.gdlgxy.oa.service.FareServer;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.TreeMap;

public class FareListCtrl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 设置响应的内容类型，防止中文乱码问题
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 获取动态应用的根路径
        String contextPath = request.getContextPath();

        PrintWriter out = response.getWriter();

        out.print("<!DOCTYPE html>");
        out.print("<html lang='zh-CN'>");
        out.print("<head>");
        out.print("    <meta charset='UTF-8'>");
        out.print("    <meta http-equiv='X-UA-Compatible' content='IE=edge'>");
        out.print("    <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.print("    <title>学生支出表</title>");
        out.println(" <style>");
        out.println("             h1 {");
        out.println("         padding: 0px 20px;");
        out.println("         border-left: 10px solid #ed515191;");
        out.println("         background-color: rgba(208, 208, 208, 0);");
        out.println("         font-size: 19px;");
        out.println("         line-height: 30px;");
        out.println("         color: cornflowerblue;");
        out.println("         font-weight: bold;");
        out.println("         margin: 5px 0;");
        out.println("     }");
        out.println("     a {");
        out.println("         text-decoration: none;");
        out.println("     }");
        out.println("     div {");
        out.println("         text-align: center;");
        out.println("         padding: 50px;");
        out.println("         width: 150px;");
        out.println("         margin: auto");
        out.println("     }");
        out.println("     /*表格样式*/");
        out.println("     table {");
        out.println("         width: 90%;");
        out.println("         background: #ccc;");
        out.println("         margin: 10px auto;");
        out.println("         border-collapse: collapse;");
        out.println("         /*border-collapse:collapse合并内外边距(去除表格单元格默认的2个像素内外边距*/");
        out.println("     }");
        out.println("     th,");
        out.println("             td {");
        out.println("         height: 25px;");
        out.println("         line-height: 30px;");
        out.println("         text-align: center;");
        out.println("         border: 1px solid #ccc;");
        out.println("     }");
        out.println("     th {");
        out.println("         background: #eee;");
        out.println("         font-weight: normal;");
        out.println("     }");
        out.println("     tr {");
        out.println("         background: #fff;");
        out.println("     }");
        out.println("     tr:hover {");
        out.println("         background: rgb(255, 255, 173);");
        out.println("     }");
        out.println("     td a {");
        out.println("         color: #06f;");
        out.println("         text-decoration: none;");
        out.println("     }");
        out.println("     td a:hover {");
        out.println("         color: white;");
        out.println("     }");
        out.println("     a {");
        out.println("         display: inline-block;");
        out.println("         margin: 0px 0px 5px 5px;");
        out.println("         padding: 6px 8px;");
        out.println("         font-size: 16px;");
        out.println("         outline: none;");
        out.println("         text-align: center;");
        out.println("         width: 70px;");
        out.println("         line-height: 16px;");
        out.println("         cursor: pointer;");
        out.println("     }");
        out.println("     a:hover {");
        out.println("         color: white;");
        out.println("         background-color: rgba(0, 106, 255, 0.8);");
        out.println("     }");
        out.println(" </style>");
        //<!--负责删除的js函数，发送请求-->
        out.print("<script type='text/javascript'>");
        out.print("    function del(Sno,Fno){");
        out.print("        var ok = window.confirm('警告：你正在删除该学生对应消费记录！！！');");
        out.print("        if(ok){");
        out.print("            document.location.href = '" + contextPath + "/fare/del?Sno='+Sno+'&Fno=' + Fno;");
        out.print("        }");
        out.print("    }");
        out.print("</script>");


        out.print("</head>");
        out.print("<body>");


        out.print("    <h1>学生支出表：</h1>");
        out.print("    <table align='center' cellpadding='0' cellspacing='0' border='1'>");
        out.print("        <title>学生表</title>");
        out.print("        <tr>");
        out.print("            <th>类型</th>");
        out.print("            <th>金额</th>");
        out.print("            <th>时间</th>");
        out.print("            <th>备注</th>");
        out.print("            <th>操作</th>");
        out.print("        </tr>");
        // 以上程序是固定的

        // 获取学生编号
        int sno = Integer.parseInt(request.getParameter("Sno"));
        // 根据学生编号查询信息
        FareList list = FareServer.list(sno);
        // 获得当前的支出类型
        TreeMap<Integer, String> map = list.getMap();
        // 设置缓冲变量
        Fare temp = list.getFirst().getNext();

        while (temp != null) {
            out.print("        <tr>");
            out.print("            <td>" + map.get(temp.getKno()) + "</td>");
            out.print("            <td>" + temp.getMoney() + "</td>");
            out.print("            <td>" + temp.getDate() + "</td>");
            out.print("            <td>" + temp.getRemark() + "</td>");
            out.print("            <td> <a href='javascript:void(0)' onclick='del(" + sno + "," + temp.getFno() + ")' >删除</a>");
            out.print("            <a href=" + contextPath + "/fare/update?Sno=" + sno + "&Fno="+temp.getFno()+" >修改</a></td>");
            out.print("        </tr>");
            temp = temp.getNext();
        }
        System.out.println("controller:输出学生链表成功！！！");

        // 以下程序是固定的
        out.print("    </table>");
        out.print("    <div>");
        out.print("        <a href='" + contextPath + "/fare/create?Sno=" + sno + "'>添加支出</a>");
        out.print("    </div>");
        out.print("        <h3><a href='" + contextPath + "/student/list'>上一页</a></h3" +
                ">");
        out.print("</body>");
        out.print("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 该方法处理post请求，其调用get请求，欺骗小猫咪
        doGet(request, response);
    }
}
